package com.dianmi.seckill.common.log.impl;

import com.dianmi.seckill.common.log.LogEntity;
import com.dianmi.seckill.common.log.LogTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/**
 * @author xieqiaofu
 * @date 2020/11/18
 */
public class LogPrinter {

    private LogContext context;

    public LogPrinter(LogContext context) {
        this.context = context;
    }

    public void addLog(LogEntity log) {
        printLog(log);
    }

    private void printLog(LogEntity log) {
        Logger logger = LoggerFactory.getLogger(log.getClazz());
        MDC.put("trace_id", context.getTraceId());
        MDC.put("method", log.getMethodName());
        if (LogTypeEnum.THROWABLE == log.getLogType()) {
            logger.error(log.getSpan(), log.getThrowable());
            return;
        }
        logger.info(log.getSpan());
    }
}
